package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description 能够初始化队列与交换机的绑定关系.
 * @Date 2023/7/25 14:10
 * @Version 1.0
 */
@Configuration
public class DeadLetterMqConfig {

    //  自定义变量：
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    /**
     * 这个方法什么意思? 创建一个交换机放入spring 容器中.
     * @return
     */
    @Bean
    public DirectExchange exchange(){
        //  创建一个交换机
        return new DirectExchange(exchange_dead,true,false);
    }

    @Bean
    public Queue queue(){
        //  做延迟消息：放入map中
        HashMap<String, Object> map = new HashMap<>();
        //  10s
        map.put("x-message-ttl",10000);
        //  设置路由键与交换机
        map.put("x-dead-letter-exchange",exchange_dead);
        map.put("x-dead-letter-routing-key",routing_dead_2);
        //  第三个参数表示是否排外.
        return new Queue(queue_dead_1,true,false,false,map);
    }

    //  绑定队列1
    @Bean
    public Binding binding(){
        //  设置绑定关系.
        return BindingBuilder.bind(queue()).to(exchange()).with(routing_dead_1);
    }

    //  创建队列2
    @Bean
    public Queue queue2(){
        return new Queue(queue_dead_2,true,false,false);
    }

    //  绑定队列2
    @Bean
    public Binding binding2(){
        //  设置绑定关系.
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }

}
